package com.library_management.model;

import java.io.Serializable;

public class User implements Serializable {
    // 1. 私有属性（对应数据库表字段）
    private Integer id;
    private String username;
    private String password;
    private String name;
    private String role;
    private Integer status;

    // 2. 无参构造器（必须提供，否则影响后续反射实例化）
    public User() {}

    // 3. getter 和 setter 方法（含验证逻辑）
    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id; // id 可空，无需验证
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        // 验证：非空且长度4-20位
        if (username == null || username.length() < 4 || username.length() > 20) {
            throw new IllegalArgumentException("username 必须非空且长度在4-20位之间");
        }
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        // 验证：非空
        if (password == null || password.trim().isEmpty()) {
            throw new IllegalArgumentException("password 不能为空");
        }
        this.password = password;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name; // name 可空，无需验证
    }

    public String getRole() {
        return role;
    }

    public void setRole(String role) {
        // 验证：只能是 admin 或 user
        if (role == null || (!"admin".equals(role) && !"user".equals(role))) {
            throw new IllegalArgumentException("role 只能是 'admin' 或 'user'");
        }
        this.role = role;
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        // 验证：只能是 0（禁用）或 1（正常）
        if (status == null || (status != 0 && status != 1)) {
            throw new IllegalArgumentException("status 只能是 0 或 1");
        }
        this.status = status;
    }

    // 4. 重写 toString()，包含所有属性
    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", name='" + name + '\'' +
                ", role='" + role + '\'' +
                ", status=" + status +
                '}';
    }
}